/**
 * 
 */
package com.effectivsocial.controller;

import java.util.Locale;

import javax.inject.Inject;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import com.effectivsocial.domain.NetworkType;
import com.effectivsocial.domain.SocialNetwork;
import com.effectivsocial.service.api.SocialNetworkService;

/**
 * @author Dhrubo
 *
 */
@Controller
@Slf4j
@RequestMapping(value="/network") 
public class CreateTwitterController {
	
	@Inject
	private SocialNetworkService socialNetworkService;
	

	@RequestMapping(value="/newtwitter") 
	public String showForm(Model model){
				
		log.info("----------------------New Social Network ---------------------------");
	
		
		model.addAttribute(new SocialNetwork());
		
		return "createtwitter";
	}
	
		
	@RequestMapping(value="/savetwitter")
	public String submitForm(@Valid SocialNetwork socialNetwork, BindingResult result, Locale locale){
		
		log.info("Saving social network - {}", socialNetwork);
		
		NetworkType networkType = new NetworkType();
		networkType.setType("NetworkType");
		networkType.setName("Twitter");
		
		
		socialNetwork.setNetworkType(networkType);
		
		if(!result.hasErrors()){
			socialNetworkService.save(socialNetwork);
		}
		
		return "createtwitter";
	}
	
	
}
